﻿using MediatR;
using Newtonsoft.Json;
using Share;
using System.Collections.Generic;
using SystemHYS.API.Application.command.ProductionOrdersCommand;
using SystemHYS.Domian;
using SystemHYS.Domian.DTO;
using SystemHYS.Infrstructre.Interface;
using static Share.ParseAPI;
namespace SystemHYS.API.Application.commandler.ProductionOrdersCommandler
{
    public class ProductionOrdersModelCommandler:IRequestHandler<ProductionOrdersModelCommand, ProductionOrdersDTO>
    {
        IProductionOrdersRepository OrdersRepository;
        IProductionOrdersFinishedProductRepository ProductionOrdersFinishedProductRepository;
        public ProductionOrdersModelCommandler(IProductionOrdersRepository ordersRepository, IProductionOrdersFinishedProductRepository productionOrdersFinishedProductRepository)
        {
            OrdersRepository = ordersRepository;
            ProductionOrdersFinishedProductRepository = productionOrdersFinishedProductRepository;
        }

        public async Task<ProductionOrdersDTO> Handle(ProductionOrdersModelCommand request, CancellationToken cancellationToken)
        {
            //成品表
            List<FinishedProduct> FinishedProductList = await GetFinishedProduct();
            //订单成品关联
            var POF=from b in await ProductionOrdersFinishedProductRepository.GetAllAsnc() select b;

            var list =(from a in await OrdersRepository.GetAllAsnc()
                       where a.ProductionOrdersId == request.ProductionOrdersId 
                       select new ProductionOrdersDTO
                       {
                           AddTime=Convert.ToDateTime(a.AddTime.Value.ToString("yyyy-MM-dd")),
                           ProductionOrdersId=a.ProductionOrdersId,
                           ProductionNumber=a.ProductionNumber,
                           Client=a.Client,
                           OrderNumber=a.OrderNumber,
                           Priority=a.Priority,
                           remark=a.remark,
                           State=a.State,
                           List=( from b in  POF
                                 join  c in  FinishedProductList on b.FinishedProductId equals c.finishedProductId 
                                where b.ProductionOrdersId==a.ProductionOrdersId
                                select new FinishedProductDTO
                                {
                                    finishedProductId=c.finishedProductId,
                                    finishedProductNumbering=c.finishedProductNumbering,
                                    finishedProductName=c.finishedProductName,
                                    finishedProductModel = c.finishedProductModel,
                                    finishedProductUnit = c.finishedProductUnit,
                                    finishedProductBatch = c.finishedProductBatch,
                                    Plannedproduction = b.Plannedproduction,
                                    Deliverydate = b.Deliverydate
                                }).ToList()
                       }).FirstOrDefault();
            return list;

        }
        public async Task<List<FinishedProduct>> GetFinishedProduct(int pageindex = 1, int pagesize = 1000)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIzMyIsInNpZCI6IjE4NjcwOTg1NDU4NTM2OTgwNDgiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiIiwianRpIjoiMTYzZWM0ZTYtMTM0Yi00NmYwLWIxYmEtMDFhNjkwNWU0OWFjIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6Iui0uuiJs-W1qSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3VyaSI6Ii8iLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.xqb5lfaYGcOKr7lXwZY5ZnAN8gzcqYfc1z9dUKieVfI";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string chanpin = "http://47.96.140.177:8000/RawApi/api/FinishedProducts/FinishedProductsShow?Pageindex=" + pageindex + "&Pagesize=" + pagesize;
            string postreonse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<FinishedProduct>>(postreonse);
            return json.data.plist;
        }
    }
}
